PartDBLabelUI   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 35
rs 10
wmc 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A init 0 33 3
1
/*
2
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
3
 *
4
 *  Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
5
 *
6
 *  This program is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU Affero General Public License as published
8
 *  by the Free Software Foundation, either version 3 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU Affero General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU Affero General Public License
17
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
21
22
require('./lang/de.js');
23
24
import { addListToDropdown, createDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
25
26
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
27
import Model from '@ckeditor/ckeditor5-ui/src/model';
28
29
export default class PartDBLabelUI extends Plugin {
30
    init() {
31
        const editor = this.editor;
32
        const t = editor.t;
33
34
        // The "placeholder" dropdown must be registered among the UI components of the editor
35
        // to be displayed in the toolbar.
36
        editor.ui.componentFactory.add( 'partdb_label', locale => {
37
            const dropdownView = createDropdown( locale );
38
39
            // Populate the list in the dropdown with items.
40
            addListToDropdown( dropdownView, getDropdownItemsDefinitions(t) );
41
42
            dropdownView.buttonView.set( {
43
                // The t() function helps localize the editor. All strings enclosed in t() can be
44
                // translated and change when the language of the editor changes.
45
                label: t( 'Label Placeholder' ),
46
                tooltip: true,
47
                withText: true
48
            } );
49
50
            // Disable the placeholder button when the command is disabled.
51
            const command = editor.commands.get( 'partdb_label' );
52
            dropdownView.bind( 'isEnabled' ).to( command );
53
54
            // Execute the command when the dropdown item is clicked (executed).
55
            this.listenTo( dropdownView, 'execute', evt => {
56
                editor.execute( 'partdb_label', { value: evt.source.commandParam } );
57
                editor.editing.view.focus();
58
            } );
59
60
            return dropdownView;
61
        } );
62
    }
63
}
64
65
const PLACEHOLDERS = [
66
    {
67
        label: 'Part',
68
        entries: [
69
            ['[[ID]]', 'Database ID'],
70
            ['[[NAME]]', 'Part name'],
71
            ['[[CATEGORY]]', 'Category'],
72
            ['[[CATEGORY_FULL]]', 'Category (Full path)'],
73
            ['[[MANUFACTURER]]', 'Manufacturer'],
74
            ['[[MANUFACTURER_FULL]]', 'Manufacturer (Full path)'],
75
            ['[[FOOTPRINT]]', 'Footprint'],
76
            ['[[FOOTPRINT_FULL]]', 'Footprint (Full path)'],
77
            ['[[MASS]]', 'Mass'],
78
            ['[[MPN]]', 'Manufacturer Product Number (MPN)'],
79
            ['[[TAGS]]', 'Tags'],
80
            ['[[M_STATUS]]', 'Manufacturing status'],
81
            ['[[DESCRIPTION]]', 'Description'],
82
            ['[[DESCRIPTION_T]]', 'Description (plain text)'],
83
            ['[[COMMENT]]', 'Comment'],
84
            ['[[COMMENT_T]]', 'Comment (plain text)'],
85
            ['[[LAST_MODIFIED]]', 'Last modified datetime'],
86
            ['[[CREATION_DATE]]', 'Creation datetime'],
87
        ]
88
    },
89
    {
90
        label: 'Part lot',
91
        entries: [
92
            ['[[LOT_ID]]', 'Lot ID'],
93
            ['[[LOT_NAME]]', 'Lot name'],
94
            ['[[LOT_COMMENT]]', 'Lot comment'],
95
            ['[[EXPIRATION_DATE]]', 'Lot expiration date'],
96
            ['[[AMOUNT]]', 'Lot amount'],
97
            ['[[LOCATION]]', 'Storage location'],
98
            ['[[LOCATION_FULL]]', 'Storage location (Full path)'],
99
        ]
100
    },
101
    {
102
        label: 'Storage location',
103
        entries: [
104
            ['[[ID]]', 'Location ID'],
105
            ['[[NAME]]', 'Name'],
106
            ['[[FULL_PATH]]', 'Full path'],
107
            ['[[PARENT]]', 'Parent name'],
108
            ['[[PARENT_FULL_PATH]]', 'Parent full path'],
109
            ['[[COMMENT]]', 'Comment'],
110
            ['[[COMMENT_T]]', 'Comment (plain text)'],
111
            ['[[LAST_MODIFIED]]', 'Last modified datetime'],
112
            ['[[CREATION_DATE]]', 'Creation datetime'],
113
        ]
114
    },
115
    {
116
        label: 'Barcodes',
117
        entries: [
118
            ['[[1D_CONTENT]]', 'Content of the 1D barcodes (like Code 39)'],
119
            ['[[2D_CONTENT]]', 'Content of the 2D barcodes (QR codes)'],
120
            ['[[BARCODE_QR]]', 'QR code linking to this element'],
121
            ['[[BARCODE_C128]]', 'Code 128 barcode linking to this element'],
122
            ['[[BARCODE_C39]]', 'Code 39 barcode linking to this element'],
123
        ]
124
    },
125
    {
126
        label: 'Globals',
127
        entries: [
128
            ['[[USERNAME]]', 'Username'],
129
            ['[[USERNAME_FULL]]', 'Username (including name)'],
130
            ['[[DATETIME]]', 'Current datetime'],
131
            ['[[DATE]]', 'Current date'],
132
            ['[[TIME]]', 'Current time'],
133
            ['[[INSTALL_NAME]]', 'Instance name'],
134
            ['[[TYPE]]', 'Target type'],
135
            ['[[INSTANCE_URL]]', 'URL of this Part-DB instance']
136
        ],
137
    },
138
];
139
140
141
function getDropdownItemsDefinitions(t) {
142
    const itemDefinitions = new Collection();
143
144
    for ( const group of PLACEHOLDERS) {
145
        //Add group header
146
        itemDefinitions.add({
147
            'type': 'separator',
148
            model: new Model( {
149
                withText: true,
150
            })
151
        });
152
153
        itemDefinitions.add({
154
            type: 'button',
155
            model: new Model( {
156
                label: t(group.label),
157
                withText: true,
158
                isEnabled: false,
159
            } )
160
        });
161
162
        //Add group entries
163
        for ( const entry of group.entries) {
164
            const definition = {
165
                type: 'button',
166
                model: new Model( {
167
                    commandParam: entry[0],
168
                    label: t(entry[1]),
169
                    tooltip: entry[0],
170
                    withText: true
171
                } ),
172
            };
173
174
            // Add the item definition to the collection.
175
            itemDefinitions.add( definition );
176
        }
177
    }
178
179
    return itemDefinitions;
180
}